/**
 * @author 黄波
 * 创建日期：2011Oct 14, 2011
 * TODO// N/A
 */
package com.platform.njcc.action;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.platform.bean.PageBean;
import com.platform.common.bo.CommonBo;
import com.platform.course.bean.CourseBean;
import com.platform.course.bo.CourseBo;
import com.platform.pojo.CourseInfo;
import com.platform.pojo.SchoolProperty;
import com.platform.user.bo.UserBo;

/**
 * 搜索课程
 */
public class SearchCourseAction extends NJCCSuport {

	private String courseQuery;
	private String courseQueryType;
	private PageBean pageBean;
	private List<CourseBean> courselist = new ArrayList<CourseBean>();	//课程互动列表
	@Override
	public String execute() throws Exception {
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
//		if(courseQuery == null || courseQuery.trim().equals("")) {
//			out.flush();
//			out.write("<script>alert('搜索条件不可为空！');history.back();</script>");
//			out.close();
//			return null;
//		}
		Object[] objs = null;
		if(courseQueryType.equals("0")) {
			//按资源名称查询
			objs = CourseBo.getInstance().getCourseListByName(courseQuery, pageBean);
		} else if(courseQueryType.equals("1")) {
			//按任课教师查询
			objs = CourseBo.getInstance().getCourseListByTeacher(courseQuery, pageBean);
		} else {
			courseQuery = "所有课程";
			objs = CourseBo.getInstance().getInfos(pageBean);
		}
		pageBean = (PageBean) objs[0];
		List<CourseInfo> infos = (List) objs[1];

		//查询课程互动信息列表
		//公共属性
		CommonBo commonBo = CommonBo.getInstance();
		if(infos != null) {
			for(CourseInfo item : infos) {
				CourseBean bean = new CourseBean();
				//课程实体
				bean.setItem(item);
				//任课老师
				bean.setTeacher(UserBo.getInstance().getUser(item.getUserId().toString()).getRealName());
				//班级名称
				SchoolProperty sp = commonBo.getSchoolProperty(item.getClassId().toString());
				if(sp != null) {
					bean.setClassName(sp.getName());
					bean.setClassNum(sp.getClassNum().toString());
				}
				courselist.add(bean);
			}
		}
		return SUCCESS;
	}
	public String getCourseQuery() {
		return courseQuery;
	}
	public void setCourseQuery(String courseQuery) {
		this.courseQuery = courseQuery;
	}
	public String getCourseQueryType() {
		return courseQueryType;
	}
	public void setCourseQueryType(String courseQueryType) {
		this.courseQueryType = courseQueryType;
	}
	public PageBean getPageBean() {
		return pageBean;
	}
	public void setPageBean(PageBean pageBean) {
		this.pageBean = pageBean;
	}
	public List<CourseBean> getCourselist() {
		return courselist;
	}
	public void setCourselist(List<CourseBean> courselist) {
		this.courselist = courselist;
	}
}
